/*
 * @Author: your name
 * @Date: 2022-01-31 15:18:29
 * @LastEditTime: 2022-03-12 18:04:38
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \vue3-ts-food-admin\src\router\router.ts
 */
import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import { getToken } from "@/utils/tool";
import { IMenubarList } from "@/types/index";

export const workspaceRoutes: IMenubarList[] = [
  {
    path: "/workspace/home",
    name: "home",

    meta: { title: "主页", icon: "home-filled" },
    component: () => import("@/views/home/home.vue"),
  },
  {
    path: "/workspace/seatmanage",
    name: "seatmanage",
    meta: { title: "餐位管理", icon: "grid" },
    component: () => import("@/views/seatManage/seatManage.vue"),
  },
  {
    path: "/workspace/ordermanage",
    name: "ordermanage",
    meta: { title: "订单管理", icon: "list" },
    component: () => import("@/views/orderManage/orderManage.vue"),
  },
  {
    path: "/workspace/dishesmanage",
    name: "dishesmanage",
    meta: { title: "菜品管理", icon: "" },
    component: () => import("@/views/dishesManage/dishesManage.vue"),
  },
];
const routes: IMenubarList[] = [
  {
    path: "/",
    name: "foodadmin",
    redirect: "/workspace",
    meta: {
      hidden: true,
      title: "",
      icon: "",
    },
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/login.vue"),
    meta: {
      hidden: true,
      title: "",
      icon: "",
    },
  },
  {
    path: "/workspace",
    name: "workspace",
    redirect: "/workspace/home",
    component: () => import("@/layout/layout.vue"),
    meta: { title: "主页", icon: "", activeMenu: "/workspace/home" },
    children: workspaceRoutes,
  },
];

const router = createRouter({
  routes: routes as RouteRecordRaw[],
  history: createWebHashHistory(),
});

router.beforeEach((to, from, next) => {
  const token = getToken();

  if (token) {
    if (to.path === "/login") {
      next("/");
    } else {
      next();
    }
  } else {
    if (to.path !== "/login") {
      next("/login");
    } else {
      next();
    }
  }
});

export default router;
